package trlygjj.tanruiliyigenjinjin.web.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import trlygjj.tanruiliyigenjinjin.domain.TuitionDetail;
import trlygjj.tanruiliyigenjinjin.page.PageResult;
import trlygjj.tanruiliyigenjinjin.query.TuitionDetaQueryObject;
import trlygjj.tanruiliyigenjinjin.service.ITuitionDetailService;
import trlygjj.tanruiliyigenjinjin.util.JsonResult;
import trlygjj.tanruiliyigenjinjin.util.PermissionName;

import java.util.List;

/**
 * 学员缴费明细
 */
@Controller
@RequestMapping("tuitionDetail")
public class TuitionDetailController {

    @Autowired
    private ITuitionDetailService tuitionDetailService;

    /**
     * 提供一个默认不输入后缀,直接可以跳转到具有分页的列表页面
     * @return
     */
    @RequestMapping("")
    @PermissionName("缴费明细视图")
    @RequiresPermissions("tuitionDetail:index")
    public String index(){
        return "tuitionDetail/list";
    }

    /**
     * 具有分页和高级查询
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    @PermissionName("缴费明细列表")
    @RequiresPermissions("tuitionDetail:list")
    public PageResult list(TuitionDetaQueryObject qo){
        PageResult result = tuitionDetailService.query(qo);
        return result;
    }

    /**
     * 查询全部
     * @return
     */
    @RequestMapping("listAll")
    @ResponseBody
    @PermissionName("缴费明细查询全部")
    @RequiresPermissions("tuitionDetail:listAll")
    public List<TuitionDetail> listAll(){
        return tuitionDetailService.selectAll();
    }

    /**
     * 删除
     * @param tuitionDetailId 要删除的学员缴费明细对象id
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequestMapping("delete")
    @ResponseBody
    @PermissionName("缴费明细删除")
    @RequiresPermissions("tuitionDetail:delete")
    public JsonResult delete(Long tuitionDetailId){
        JsonResult result = new JsonResult();
        try{
            tuitionDetailService.deleteByPrimaryKey(tuitionDetailId);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }

    /**
     *更新
     * @param tuitionDetail 要更新的学员缴费明细对象
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequestMapping("update")
    @ResponseBody
    @PermissionName("缴费明细更新")
    @RequiresPermissions("tuitionDetail:update")
    public JsonResult update(TuitionDetail tuitionDetail){
        JsonResult result = new JsonResult();
        try{
            tuitionDetailService.updateByPrimaryKey(tuitionDetail);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }

    /**
     * 保存
     * @param tuitionDetail 要保存的学员缴费明细对象
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequestMapping("save")
    @ResponseBody
    @PermissionName("缴费明细更新")
    @RequiresPermissions("tuitionDetail:update")
    public JsonResult save(TuitionDetail tuitionDetail){
        JsonResult result = new JsonResult();
        try{
            tuitionDetailService.insert(tuitionDetail);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }

    /**
     * 审核收款记录
     *
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequestMapping("changeState")
    @ResponseBody
    @PermissionName("缴费明细审核")
    @RequiresPermissions("tuitionDetail:changeState")
    public JsonResult changeState(Long id){
        JsonResult result = new JsonResult();
        try{
            // 查询出需要审核退款状态
            tuitionDetailService.updateTuitionDetailState(id);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }
}
